#include "thread.h"
#include "ui_thread.h"
#include <QThread>
#include <QMessageBox>

Thread::Thread(QWidget *parent) :
	QWidget(parent),
	ui(new Ui::Thread)
{
	ui->setupUi(this);
	this->setWindowTitle("Thread");
	this->resize(1200,600);

	timer = new QTimer(this);
	static int i = 0;
	connect(timer,&QTimer::timeout,this,[=](){
		++i;
		ui->lcdNumber->display(i);
	});
	//分配空间
	thread = new MyThread(this);
	//接收信号
	connect(thread,&MyThread::idDone,this,[=](){
		timer->stop();
		this->setWindowTitle("线程结束");
	});
	//销毁线程
	connect(this,&MyThread::destroyed,this,[=](){
		thread->quit();
		thread->wait();
		QMessageBox::question(this,"question","This is a question Dialog!",QMessageBox::Ok | QMessageBox::Cancel,QMessageBox::Cancel);
		QMessageBox::information(this,"information","This is a information Dialog!",QMessageBox::Ok | QMessageBox::Cancel,QMessageBox::Cancel);
		QMessageBox::warning(this,"warning","This is a warning Dialog!",QMessageBox::Ok | QMessageBox::Cancel,QMessageBox::Cancel);
		QMessageBox::critical(this,"critical","This is a critical Dialog!",QMessageBox::Ok | QMessageBox::Cancel,QMessageBox::Cancel);
		QMessageBox::about(this,"about","This is a about Dialog!");
	});
}

Thread::~Thread()
{
	delete ui;
}

void Thread::on_pushButton_clicked()
{
	timer->start(100);
	thread->start();
}
